package
{
	import com.mxprojects.gui.BreadCrumbs;
	import com.mxprojects.gui.Resizer;
	import com.mxprojects.io.DeepLinking;
	
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.net.URLRequest;
	import flash.text.TextField;

	public class MXP3Header extends MovieClip
	{		
		private const GAP:String = "    ";
		private const CUSTOM_LINK_ELEMENTS:Array = new Array(); //example add "about" for an about page
		
		/********************************************************
		 * To add more sections use the following example:
		 * "<a href=\"event:About\">ABOUT</a>" + GAP + 
		 * You will need to add a reference to it in "parseNav"
		 *******************************************************/
		public const NAV_TEXT:String = "<font color=\"#CCCCCC\">" +
									   "<a href=\"event:Home\">HOME</a>" +
									   "</font>";
		
		public var background:MovieClip;
		public var logo:MovieClip;
		public var line:MovieClip;
		
		private var siteNav:TextField;
		private var formattedArea:String;
		
		public function MXP3Header()
		{	
			//setup the nav text
			siteNav = logo.getChildByName("siteNav") as TextField;
			//uncomment if you want a site nav in your header
			//siteNav.htmlText = "<b>" + NAV_TEXT + "</b>";
			
			addEventListener(Event.ADDED_TO_STAGE, addToStage, false, 0, true);
			
			//setup the logo link
			var homeButton:MovieClip = logo.getChildByName("homeButton") as MovieClip;
			homeButton.buttonMode = true;
			homeButton.addEventListener(MouseEvent.CLICK, goHome, false, 0, true);
		}
		
		private function addToStage(event:Event):void {
			//listen for stage resizing
			stage.addEventListener(Event.RESIZE, resizeBackground, false, 0, true);
			resizeBackground();
			
			//listen for clicks
			siteNav.addEventListener(TextEvent.LINK, parseNav, false, 0, true);
			
			//tell deep linking about our function
			DeepLinking.customFunction = buildContent;
			
			//if we've loaded with one of our links, let's handle it!
			if(CUSTOM_LINK_ELEMENTS.indexOf(DeepLinking.getValue()) != -1){
				buildContent(new Array(DeepLinking.getValue()));
			}
		}
		
		private function resizeBackground(event:Event = null):void {
			var wStage:int = stage.stageWidth;
			background.width = wStage;
			line.width = wStage - line.x*2;
			//logo.x = Math.round(wStage/2 - logo.width/2);
		}
		
		private function parseNav(event:TextEvent):void {
			switch(event.text){
				case "Home":
					goHome();
				break;
				
				//left About as an example on using the content-template
				//see ContentTemplate.fla
				case "About":
					loadContent(event.text);
				break;
			}
		}
		
		private function loadContent(area:String):void {
			DeepLinking.setLink(DeepLinking.CUSTOM_LINK, new Array(area.toLocaleLowerCase(), buildContent));
		}
		
		private function buildContent(...args:Array):void {
			//empty the stuff
			MXP3.clearBoard();
			
			var area:String = args[0];
			formattedArea = area.substr(0,1).toLocaleUpperCase() + area.substr(1);
			
			//load it up
			var loader:Loader = new Loader();
			loader.load(new URLRequest("img/content-template.swf"));
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, showContent, false, 0, true);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, error, false, 0, true);
			
			BreadCrumbs.createCrumbs(0, "", 0, "", DeepLinking.CUSTOM_LINK + "," + area + "," + buildContent, formattedArea);
			DeepLinking.setTitle(formattedArea);
			
		}
		
		private function showContent(event:Event):void {
			var holder:Sprite = resetHolder();
			var content:ContentTemplate = event.currentTarget.content as ContentTemplate;
			
			//show the stuff
			content.showSection(formattedArea);
			
			holder.addChild(content);
			MXP3.addContent(holder);
			
			//do a quick resize
			Resizer.resizeContent();
		}
		
		private function resetHolder():Sprite {
			var newHolder:Sprite = new Sprite();
			newHolder.name = "holder";
			
			var oldHolder:Sprite = MXP3.getContent("holder") as Sprite;
			if(oldHolder != null){	
				MXP3.contentHolder.removeChild(oldHolder);
			}
			
			return newHolder;
		}
		
		private function goHome(event:MouseEvent = null):void {
			DeepLinking.setLink("forums", DeepLinking.createLinkArray());
		}
				
		private function error(event:IOErrorEvent):void {
			//error
		}
		
	}
}
